<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车主姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入车主姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlateNum">
        <el-input
          v-model="queryParams.licensePlateNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="ownerContact">
        <el-input
          v-model="queryParams.ownerContact"
          placeholder="请输入车主联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="开始日期" prop="vipStartDate">
        <el-date-picker clearable
          v-model="queryParams.vipStartDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择VIP开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="vipEndDate">
        <el-date-picker clearable
          v-model="queryParams.vipEndDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择VIP结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="费用" prop="chargeAmount">
        <el-input
          v-model="queryParams.chargeAmount"
          placeholder="请输入充卡的费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:vehicleownerinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:vehicleownerinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:vehicleownerinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:vehicleownerinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleownerinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="车主ID" align="center" prop="userId" /> -->
      <el-table-column label="车主姓名" align="center" prop="username" />
      <el-table-column label="车牌号" align="center" prop="licensePlateNum" />
      <el-table-column label="车主联系方式" align="center" prop="ownerContact" />
      <el-table-column label="办卡类型" align="center" prop="vipType" width="120">
  <template slot-scope="scope">
    <el-button
      :type="scope.row.vipType === 0 ? 'success' : 'primary'"
      :plain="true"
      size="small"
    >
      {{ scope.row.vipType === 0 ? '年卡' : '月卡' }}
    </el-button>
  </template>
</el-table-column>
      <el-table-column label="VIP开始日期" align="center" prop="vipStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vipStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="VIP结束日期" align="center" prop="vipEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vipEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充卡的费用" align="center" prop="chargeAmount" />
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:vehicleownerinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:vehicleownerinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆充卡对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车主姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入车主姓名" />
        </el-form-item>

        <el-form-item label="车牌号" prop="licensePlateNum">
          <el-input v-model="form.licensePlateNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="联系方式" prop="ownerContact">
  <el-input v-model.trim="form.ownerContact" placeholder="请输入车主联系方式" @blur="validateContact" />
</el-form-item>
        <el-form-item label="办卡类型" prop="vipType">
  <el-select v-model="form.vipType" placeholder="请选择办卡类型" clearable @change="handleVipTypeChange">
    <el-option
      v-for="item in vipTypeOptions"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    ></el-option>
  </el-select>
</el-form-item>
<el-form-item label="开始日期" prop="vipStartDate">
  <el-date-picker
    clearable
    v-model="form.vipStartDate"
    type="date"
    value-format="yyyy-MM-dd"
    placeholder="请选择VIP开始日期"
    :disabled-date="disabledStartDate"
  >
  </el-date-picker>
</el-form-item>
<el-form-item label="结束日期" prop="vipEndDate">
  <el-date-picker
    clearable
    v-model="form.vipEndDate"
    type="date"
    value-format="yyyy-MM-dd"
    placeholder="请选择VIP结束日期"
    :disabled-date="disabledEndDate"
  >
  </el-date-picker>
</el-form-item>
        <el-form-item label="费用" prop="chargeAmount">
          <el-input v-model="form.chargeAmount" placeholder="请输入充卡的费用" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVehicleownerinfo, getVehicleownerinfo, delVehicleownerinfo, addVehicleownerinfo, updateVehicleownerinfo } from "@/api/car/vehicleownerinfo";

export default {
  name: "Vehicleownerinfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆充卡表格数据
      vehicleownerinfoList: [],
      vipTypeOptions: [
      { value: 0, label: '年卡' },
      { value: 1, label: '月卡' }
    ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        licensePlateNum: null,
        ownerContact: null,
        vipType: null,
        vipStartDate: null,
        vipEndDate: null,
        chargeAmount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "车主姓名不能为空", trigger: "blur" }
        ],
        licensePlateNum: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStatusChange(status) {
    // 根据选择的设备状态做相应的处理
    console.log('设备状态变更为:', status);
  },
    /** 查询车辆充卡列表 */
    getList() {
      this.loading = true;
      listVehicleownerinfo(this.queryParams).then(response => {
        this.vehicleownerinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        username: null,
        licensePlateNum: null,
        ownerContact: null,
        vipType: null,
        vipStartDate: null,
        vipEndDate: null,
        chargeAmount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆充卡";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getVehicleownerinfo(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆充卡";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateVehicleownerinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVehicleownerinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除车辆充卡编号为"' + userIds + '"的数据项？').then(function() {
        return delVehicleownerinfo(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/vehicleownerinfo/export', {
        ...this.queryParams
      }, `vehicleownerinfo_${new Date().getTime()}.xlsx`)
    },
    //电话号码
    validateContact() {
    const phoneRegex = /^1\d{10}$/;
    if (this.form.ownerContact && !phoneRegex.test(this.form.ownerContact)) {
      this.$message.error('请输入正确的手机号码');
    }
  },
    //办卡类型的日期
     handleVipTypeChange(value) {
      this.form.vipEndDate = null;
    this.form.vipStartDate = new Date();
    if (value === 'year') {
      this.disabledStartDate = (date) => {
        return date.getFullYear() !== new Date().getFullYear();
      };
      this.disabledEndDate = (date) => {
        return date.getFullYear() !== new Date().getFullYear() + 1;
      };
    } else if (value === 'month') {
      this.disabledStartDate = (date) => {
        return date.getFullYear() !== new Date().getFullYear() || date.getMonth() !== new Date().getMonth();
      };
      this.disabledEndDate = (date) => {
        return date.getFullYear() !== new Date().getFullYear() || date.getMonth() !== new Date().getMonth() + 1;
      };
    }
  },
  disabledStartDate(date) {
    return false;
  },
  disabledEndDate(date) {
    return false;
  }
  }
};
</script>
